package stz.flash.main.state;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.decor.BackgroundFactory;
import stz.flash.Info;
import stz.flash.main.CloseHandle;
import stz.flash.main.FlashScreen;
import stz.flash.main.FlashTimer;
import stz.flash.main.MainManager;
import stz.flash.main.ManagerOfManager;
import stz.flash.main.strobe.field.StrobeControlBar;
import stz.flash.option.Option;
import stz.flash.option.OptionScreen;
import stz.flash.option.PersistentData;
import stz.flash.option.TimerDurationController;


public class KeyOperator {

	public static final int RESET_TIMER_FRONT_KEY;
	static {
		if (Info.isPearl3G() == false) 
			RESET_TIMER_FRONT_KEY = Characters.LATIN_CAPITAL_LETTER_W;
		else 
			RESET_TIMER_FRONT_KEY = Characters.LATIN_CAPITAL_LETTER_E;
	}

	private static ModeState state;
	private static ModeState nextState;
	
	private static ManagerOfManager managerOfManager;
	
	public static void analyzeLocalKey(int key) {
		
		if (isInUseGlobalKey(key)){
			return;
		}
		
		//기능이 일정함
		if (key == Characters.LATIN_CAPITAL_LETTER_O) {
			
			OptionScreen.pushScreen();
			return;
			
		} else if (key == Characters.LATIN_CAPITAL_LETTER_Q) {
			
			TimerDurationController.increaseTimerDuration();
			return;

		} else if (key == Characters.LATIN_CAPITAL_LETTER_A) {
			
			TimerDurationController.decreaseTimerDuration();
			return;
			
		} else if (key == RESET_TIMER_FRONT_KEY) {
			
			FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_timer_reset);
			return;
			
		} else if (key == Keypad.KEY_ESCAPE || key == Keypad.KEY_END) {
		
			if (OptionScreen.isOptionDisplayed())
				return;
			CloseHandle.close();
			return;
			
		
		//State에 따라 기능이 변함
		} else if (key == Characters.LATIN_CAPITAL_LETTER_C) {
			
			runC();
			return;
			
		} else if (key == Characters.SPACE) {
			
			runSpace();
			return;
		
		//State 중에 한 곳에서만 일을 함
		} else if (FlashModeState.getIns() == nextState){
			
			if (key == Characters.LATIN_CAPITAL_LETTER_L) {
				
				runL();
				return;
				
			} else if (key == Characters.LATIN_CAPITAL_LETTER_B) {
				
				runB();
				return;
				
			} 
		}
		
		
		if (key != Keypad.KEY_ENTER) {
			managerOfManager.displayHotkeyAdvice();
		}
			
		testtest(key);
	}

	public static void analyzeGlobalKey(int key){
		
		if (isInUseGlobalKey(key) == false)
			return;

		if (key == Keypad.KEY_VOLUME_UP) {

			runVolumeUp();
			return;

		} else if (key == Keypad.KEY_VOLUME_DOWN) {

			runVolumeDown();
			return;

		} else if (key == Keypad.KEY_SPEAKERPHONE || key == Keypad.KEY_LOCK) {

			if (Info.isPearl3G() == false) {

				if (key == Keypad.KEY_SPEAKERPHONE) {

					runSpeakerphone();
					return;
					
				} else if (key == Keypad.KEY_LOCK) {

					managerOfManager.toggleHoldManager();
					return;
				}

			} else {

				if (key == Keypad.KEY_SPEAKERPHONE) {
					
					managerOfManager.toggleHoldManager();
					return;
				}
			}
			
		//Storbe Keys = 일반 mute / 펄 backward, forward 
		} else if (key == Option.getIns().getStrobeToggleKey()) {

			if (state == StrobeModeState.getIns()) {
				runSpeakerphone();
				return;
			}

		}

		managerOfManager.displayHotkeyAdvice();
	}

	private static boolean isInUseGlobalKey(int key) {
		if (key == Keypad.KEY_LOCK ||
				key == Keypad.KEY_SPEAKERPHONE ||
				key == Keypad.KEY_VOLUME_UP || 
				key == Keypad.KEY_VOLUME_DOWN ||
				key == Keypad.KEY_CONVENIENCE_1 ||
				key == Keypad.KEY_CONVENIENCE_2 ||
				key == Keypad.KEY_BACKWARD ||
				key == Keypad.KEY_FORWARD ) {
			return true;
		}
		return false;
	}
	
	
	public static void runC() { 
		state.runC();
	}

	public static void runL() {
		state.runL();
	}
	
	public static void runB() {
		state.runB();
	}
	
	public static void runSpace() {
		state.runSpace();
	}
	
	public static void runSpeakerphone() {
		state.runSpeakerphone();
	}
	
	public static void runVolumeUp(){
		state.runVolumeUp();
	}
	
	public static void runVolumeDown(){
		state.runVolumeDown();
	}
	

	public static void setState(ModeState state) {
		KeyOperator.state = state;
	}
	
	public static void setNextState(ModeState state) {
		nextState = state;
		saveLatestMode(state);
	}
	public static ModeState getNextState() {
		return nextState;
	}

	private static void saveLatestMode(ModeState state) {
		if (state == AnimateModeState.getInstance())
			return;
		if (state == FlashModeState.getIns()) {
			Option.getIns().setLatstMode(Option.FLASHLIGHT_MODE);
		} else {
			Option.getIns().setLatstMode(Option.STROBE_MODE);
		}
	}
	
	public static void setManagerOfManager(ManagerOfManager managerOfManager) {
		KeyOperator.managerOfManager = managerOfManager;
	}

	private static void testtest(int key) {
		/**
		 * test
		 */
		PersistentData data = PersistentData.getPersistentDatabase();
		if (key == Characters.LATIN_CAPITAL_LETTER_T) {
			FlashScreen.insertConsol("@FlashThreadState = " + data.isFlashlightThreadState() +
					"\nTimerThreadState = " + data.isTimerThreadState());
		}
		if (key == Characters.LATIN_CAPITAL_LETTER_Y) {
			data.setTestActivity(true);
			FlashScreen.insertConsol("activity = true");
		}
		if (key == Characters.LATIN_CAPITAL_LETTER_U) {
			data.setTestActivity(false);
			FlashScreen.insertConsol("activity = false");
		}
		
		if (key == Characters.LATIN_CAPITAL_LETTER_N) {
			StrobeControlBar.getIns().moveWithIndex(1, false);
		}
		
		if (key == Characters.LATIN_CAPITAL_LETTER_M) {
			StrobeControlBar.getIns().moveWithIndex(5, true);
		}
		
		if (key == Characters.LATIN_CAPITAL_LETTER_I) {
			MainManager.getIns().getTestField().setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 230));
		}
		if (key == Characters.LATIN_CAPITAL_LETTER_K) {
			MainManager.getIns().getTestField().setBackground(null);
		}
	}
}


		
